using System;
using System.Collections;
using System.Data;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A collection of the field usage within an action. Any fields not referenced in this collection will be set to optional.
	/// </summary>
	public sealed class FieldUsageCollection : DictionaryBase, IDictionary
	{
		internal FieldUsageCollection(IDataReader reader)
		{
      AddFields(reader["eReadOnlyFields"].ToString(), FieldUsage.ReadOnly);
      AddFields(reader["eRequiredFields"].ToString(), FieldUsage.Required);
      AddFields(reader["eHiddenFields"].ToString(), FieldUsage.Hidden);
		}

    private void AddFields(string dbValue, FieldUsage usage)
    {
      string[] splitFields = dbValue.Split(',');
      for (int i = 0; i < splitFields.Length; i++)
      {
        if (!string.IsNullOrEmpty(splitFields[i]))
          if (!((IDictionary)this).Contains(splitFields[i]))
            ((IDictionary)this).Add(splitFields[i], usage);
      }
    }

    bool IDictionary.IsFixedSize
    {
      get
      {
        return true;
      }
    }

    bool IDictionary.IsReadOnly
    {
      get
      {
        return true;
      }
    }

    /// <summary>
    /// Gets the usage of the field with the specified name. If an invalid name is specified, the default field usage of optional will be returned.
    /// </summary>
    public FieldUsage this[string name]
    {
      get
      {
        if (((IDictionary)this)[name] == null)
          return FieldUsage.Optional;
        else
          return (FieldUsage)((IDictionary)this)[name];
      }
    }
	}
}
